package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.PageBean;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;

    /**
     * Controller要多个返回值，return 一个封装（两个结果），
     * 凑结果
     *
     * @param name
     * @param gender
     * @param begin
     * @param end
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageBean<Emp> selectPage(String name,
                                    Integer gender,
                                    LocalDate begin,
                                    LocalDate end,
                                    Integer page,
                                    Integer pageSize) {

        // 1. 不需要计算count
        // 2. 不需要分页了
        PageHelper.startPage(page, pageSize);
        // 1. 员工信息 - 分页
        Page<Emp> empPage = empMapper.selectList(name, gender, begin, end);

        PageBean<Emp> empPageBean = new PageBean<>();
        empPageBean.setRows(empPage.getResult());
        empPageBean.setTotal(empPage.getTotal());
        return empPageBean;
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        empMapper.deleteByIds(ids);
    }

    @Override
    public void create(Emp emp) {
        emp.setPassword("123456");
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insert(emp);
    }

    @Override
    public Emp queryById(Integer id) {
        Emp emp = empMapper.selectById(id);
        emp.setPassword(null);
        return emp;
    }

    @Override
    public void updateById(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateById(emp);
    }

    @Override
    public Emp login(String username, String password) {
        return empMapper.selectByUsernameAndPassword(username, password);
    }

    @Override
    public void removeByDeptId(Integer deptId) {
        empMapper.deleteByDeptId(deptId);
    }
}
